package labox.innovation.gameserver.network.clientpackets;

import labox.innovation.gameserver.model.ForgeItemList;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.ForgeItemSucess;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.templates.item.ItemCompositeManager;

/**
 * 客户端 合成道具
 * @author zjj
 *
 */
public class RequestCompositeItem extends L2GameClientPacket {

	private static final String RequestCompositeItem = "[C] 0x7d RequestCompositeItem";

	private long _mainId; //主物品uid

//	private int _itemNums; //辅助装备的个数

	private long[] _otherIds; //辅助装备的uid

	@Override
	public String getType() {
		return RequestCompositeItem;
	}

	@Override
	protected void readImpl() {
//		_mainId = readLong();
//		_itemNums = readByte();
//		if (_itemNums > 0) {
//			_otherIds = new long[_itemNums];
//		}
//		for (int i = 0; i < _itemNums; i++) {
//			_otherIds[i] = readLong();
//		}

	}

	@Override
	protected void runImpl() {

		FuzePcInstance player = getClient().getActiveChar();
		if (player == null) {
			return;
		}
		
		ForgeItemList forgeList = player.getActiveForgeItemList();
		if( forgeList == null )
		{
			return;
		}
		
		if( forgeList.getForgeType() != RequestForgeItem.OP_COMPOSITIE )
		{
			System.out.println("合成链不对");
			return;
		}
		
		_mainId = forgeList.getMainItemId();
		_otherIds =  forgeList.getItemIds();
		
		if( !ItemCompositeManager.getInstance().canComposite(player, _mainId, _otherIds) ) 
		{
			//不能合成
			player.sendPacket( new SystemMessage( SystemMessageId.ITEM_CANOT_COMPOSITE ) );
			return ;
		}

		L2ItemInstance oldItem = player.getInventory().getItemByObjectId( _mainId );
		L2ItemInstance newItem = ItemCompositeManager.getInstance().compositeItem(player, _mainId, _otherIds);
		if (newItem == null) {
			//TODO:
//			player.sendPacket( new SystemMessage( SystemMessageId.ITEM_FORGE_FAILED));
			player.sendPacket( new ForgeItemSucess(player, oldItem, RequestForgeItem.OP_COMPOSITIE ,0 )) ;
			player.getActiveForgeItemList().clear();
			//合成失败  系统消息
		}
		else
		{
			player.sendPacket( new ForgeItemSucess(player, newItem, RequestForgeItem.OP_COMPOSITIE ,1 )) ;
			player.getActiveForgeItemList().clear();
		}

		
	}

}
